package com.sale.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.system.domain.dto.MaterialWasteDto;
import com.sale.system.domain.vo.MaterialWasteVo;
import com.sale.system.service.IMaterialWasteService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @Description 废料信息
 * @Date 22-11-24
 * @Author hl
 **/
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/waste")
public class MaterialWasteController extends BaseController {

    private final IMaterialWasteService materialWasteService;


    /**
     * 获取废料配置
     */
    @SaCheckPermission("material:waste:list")
    @GetMapping("/list")
    public TableDataInfo<MaterialWasteDto> list(MaterialWasteVo materialWasteVo, PageQuery pageQuery)
    {
        return materialWasteService.selectWasteList(materialWasteVo,pageQuery);
    }

    /**
     * 新增废料信息
     */
    @SaCheckPermission("material:waste:add")
    @Log(title = "废料信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody MaterialWasteVo materialWasteVo) throws Exception {
        return toAjax(materialWasteService.insertWaste(materialWasteVo));
    }

    /**
     * 删除废料信息
     */
    @SaCheckPermission("material:waste:remove")
    @Log(title = "废料信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialId}")
    public R<Void> remove(@PathVariable("materialId") String materialId) throws Exception {
        return toAjax(materialWasteService.deleteWasteById(materialId));
    }

    /**
     * 根据编号获取详细信息
     */
    @GetMapping(value = "/{materialId}")
    public R getWasteInfo(@PathVariable long materialId)
    {
        return R.ok(materialWasteService.selectWasteById(materialId));
    }

    /**
     * 修改废料信息
     */
    @SaCheckPermission("material:waste:edit")
    @Log(title = "废料信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@RequestBody MaterialWasteVo materialWasteVo) throws Exception {
        return toAjax(materialWasteService.updateWasteEdit(materialWasteVo));
    }


    /**
     * 废料冻结
     */
    @Log(title = "成品信息", businessType = BusinessType.UPDATE)
    @PutMapping("/editFreeze/{materialId}")
    public R editFreeze(@PathVariable Long materialId) {
        return R.ok(materialWasteService.updateEditFreeze(materialId));
    }

    /**
     * 废料解冻
     */
    @PutMapping("/editThaw/{materialId}")
    public R editThaw(@PathVariable Long materialId) {
        return R.ok(materialWasteService.updateEditThaw(materialId));
    }



    /**
     * 生成废料导入模板
     */
    @PostMapping("/downTemplate")
    public void downTemplate(HttpServletResponse response) throws IOException {
        String fileName = "C:/Users/jack/Desktop/导入导出模板/导入模板/导入废料信息.xlsx";
        File file = new File(fileName);
        ByteArrayOutputStream baos = null;
        ServletOutputStream out = null;
        try {

            InputStream inStream=new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len;
            baos = new ByteArrayOutputStream();
            while ((len = inStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode("导入废料信息.xlsx","UTF-8"));
            out = response.getOutputStream();
            out.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baos.flush();
            baos.close();
            out.flush();
            out.close();
        }
    }

    /**
     * 废料信息导入
     */
/*    @PostMapping("/materialWasteImport")
    @RequiresPermissions("material:waste:import")
    @Log(title = "废料信息导入", businessType = BusinessType.INSERT)
    public AjaxResult materialWasteImport(MultipartFile file) throws IOException {
        ExcelUtil<MaterialWasteExport> excelUtil = new ExcelUtil<MaterialWasteExport>(MaterialWasteExport.class);
        List<MaterialWasteExport> coCustomer = excelUtil.importExcel(file.getInputStream());
        return AjaxResult.success(materialWasteService.importMouldData(coCustomer));
    }*/

}
